package com.book.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.book.common.annotation.Log;
import com.book.common.annotation.group.InsertGroup;
import com.book.common.annotation.group.UpdateGroup;
import com.book.common.core.controller.BaseController;
import com.book.common.core.domain.AjaxResult;
import com.book.common.enums.BusinessType;
import com.book.common.utils.DateUtils;
import com.book.common.utils.StringUtils;
import com.book.system.domain.Task;
import com.book.system.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bigboss
 * @create 2024-11-10 16:51
 */
@Api(tags = "任务管理")
@RestController
@RequestMapping("/system/task")
public class SysTaskController extends BaseController {

    private final ITaskService taskService;

    public SysTaskController(ITaskService taskService) {
        this.taskService = taskService;
    }

    /**
     * 获取任务列表
     * @return
     */
    @ApiOperation(value = "获取任务列表，权限：system:task:list")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public AjaxResult list(Task entity)
    {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<Task>().orderByAsc(Task::getCreateTime);
        if(StringUtils.isNotEmpty(entity.getTaskType())){
            queryWrapper.eq(Task::getTaskType,entity.getTaskType());
        }
        if(StringUtils.isNotEmpty(entity.getTaskName())){
            queryWrapper.like(Task::getTaskName,entity.getTaskName());
        }
        return success(taskService.list(queryWrapper));
    }


    /**
     * 新增任务
     */
    @ApiOperation(value = "新增任务，权限：system:task:add")
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated({InsertGroup.class}) @RequestBody Task entity)
    {
        //1.参数校验
        if(entity.getTaskReward().intValue() <= 0){
            return error("任务奖励需大于0");
        }
        if("2".equals(entity.getTaskType()) && ObjectUtil.isNull(entity.getTaskTime())){
            return error("请输入任务时长");
        }
        if("2".equals(entity.getTaskType()) && ObjectUtil.isNotNull(entity.getTaskTime()) && entity.getTaskTime().intValue() < 1){
            return error("任务时长需大于0");
        }
        //2.开始新增
        if("1".equals(entity.getTaskType())){
            entity.setTaskTime(null);
        }
        entity.setCreateBy(getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return toAjax(taskService.save(entity));
    }

    /**
     * 修改任务
     */
    @ApiOperation(value = "修改任务，权限：system:task:edit")
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated({UpdateGroup.class}) @RequestBody Task entity)
    {
        //1.参数校验
        if(entity.getTaskReward().intValue() <= 0){
            return error("任务奖励需大于0");
        }
        if("2".equals(entity.getTaskType()) && ObjectUtil.isNull(entity.getTaskTime())){
            return error("请输入任务时长");
        }
        if("2".equals(entity.getTaskType()) && ObjectUtil.isNotNull(entity.getTaskTime()) && entity.getTaskTime().intValue() < 1){
            return error("任务时长需大于0");
        }
        //2.开始修改
        if("1".equals(entity.getTaskType())){
            entity.setTaskTime(null);
        }
        entity.setUpdateBy(getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return toAjax(taskService.updateById(entity));
    }



    /**
     * 删除任务
     */
    @ApiOperation(value = "删除任务，权限：system:task:remove")
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(taskService.removeById(id));
    }
}
